import {reqAddRoomType, reqDeleteRoomType, reqGetRoomTypeListData, reqModifyRoomTypeConfig} from "@/api";

const state = {
    roomTypeList: {}
}
const actions = {
    // 新建房间类型
    async addRoomType({commit}, config){
        let result = await reqAddRoomType(config);
        if (result.code == 200){
            this._vm.$message({
                showClose:true,
                message:'添加房间类型成功！',
                type:"success"
            })
        }else{
            this._vm.$message({
                showClose:true,
                message:'添加房间类型失败，请联系管理员。',
                type:"error"
            })
        }
    },

    // 获取房间类型列表
    async getRoomType({commit}){
        let result = await reqGetRoomTypeListData();
        if (result.code == 200){
            commit('GETROOMTYPE', result.data);
        }else{
            this._vm.$message({
                showClose:true,
                message:'添加房间类型失败，请联系管理员。',
                type:"error"
            })
        }
    },

    // 修改房间类型配置
    async modifyRoomType({commit}, config){
        let result = await reqModifyRoomTypeConfig(config);
        if (result.code == 200){
            this._vm.$message({
                showClose:true,
                message:'修改房间类型成功！',
                type:"success"
            })
        }else{
            this._vm.$message({
                showClose:true,
                message:'修改房间类型失败，请联系管理员。',
                type:"error"
            })
        }
    },

    // 删除房间类型
    async deleteRoomType({commit}, typeId){
        let result = await reqDeleteRoomType(typeId);
        if (result.code == 200){
            this._vm.$message({
                showClose:true,
                message:'删除房间类型成功！',
                type:"success"
            })
        }else{
            this._vm.$message({
                showClose:true,
                message:'删除房间类型失败，请联系管理员。',
                type:"error"
            })
        }
    }
}
const mutations = {
    GETROOMTYPE(state, data){
        console.log('roomType---roomTypeList数据获取成功。');
        state.roomTypeList = data;
    }
}
const getters = {
    // 返回一个{{typeNum, typeName, typePrice}}
    getTypeList(){
        return Object.forEach(state.roomTypeList, (item, index, key)=>{
            return Object.assign({}, {
                typeNum:item.typeNum,
                typeName:item.typeName,
                typePrice:item.typePrice
            })
        })
    },
    // 返回一个{0:typeName, 1:typeName,...}
    getTypeNameList(){
        return  Object.forEach(state.roomTypeList, (item, index, key)=>{
            return item.typeName;
        })

    },
    // 返回一个类别筛选配置[{text:typeName, value:typeNum}]
    getTypeFilterConfig(){
        return Object.forEachToArr(state.roomTypeList, (item, index, key)=>{
            return Object.assign({}, {
                text:item.typeName,
                value:item.typeNum
            })
        })
    }
}

export default {
    state,
    actions,
    mutations,
    getters
}